package com.token.iot.bratterycharging.gateway;

import org.springframework.stereotype.Component;

import com.outstanding.framework.base.SpringContextHolder;
import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.gateway.dao.InvalidOperDAO;

@Component
public class SenderFactory {

	static public Sender createSender(String code, String imei) throws PendingException {
		Sender sender = null;
		//优先获取独立的下行处理对象
		try {
			sender = (Sender) SpringContextHolder.getBean(code + "Sender");

		} catch (Exception e) {
			// e.printStackTrace();
		}
		
		//当下行处理对象为空时，则获取双向处理对象
		if (sender == null) {
			try {
				sender = (Sender) SpringContextHolder.getBean(code + "DuplexProcessor");
			} catch (Exception e) {
				// e.printStackTrace();
			}
		}
		if (sender == null) {
			InvalidOper inOper = new InvalidOper();
			inOper.setImei(imei);
			inOper.setContext("下行指令");
			inOper.setOper(code);
			InvalidOperDAO dao = (InvalidOperDAO) SpringContextHolder.getBean(InvalidOperDAO.class);
			dao.insert(inOper);
			throw new PendingException(ResCode.SENDERNOTFIND);
		}
		sender.setCode(code);
		return sender;

	}
	
	
	
}
